package com.xiaojf.xlog.configure;

import com.xiaojf.xlog.annotation.EnableLogSystem;
import com.xiaojf.xlog.aspect.AccessLogAspect;
import com.xiaojf.xlog.configure.props.AccessLogProperties;
import com.xiaojf.xlog.configure.props.XlogPoolProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import javax.annotation.PostConstruct;

/**
 * 日志配置
 *
 * @author Ade.Xiao 2021/3/11 14:32
 */
@ConditionalOnBean(annotation = EnableLogSystem.class)
@Configuration
@EnableConfigurationProperties({AccessLogProperties.class, XlogPoolProperties.class})
public class LogSystemConfigure {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private XlogPoolProperties poolProperties;

    @PostConstruct
    public void postConstruct() {
        logger.info("完成初始化日志系统 ...");
    }

    /**
     * 初始化访问日志切面对象
     *
     * @return {@link AccessLogAspect}
     * @author Ade.Xiao 2021-03-11 17:12:39
     */
    @Bean
    public AccessLogAspect accessLogAspect() {
        return new AccessLogAspect();
    }

    @Bean("xlogAsyncTaskExecutor")
    public AsyncTaskExecutor asyncTaskExecutor() {
        ThreadPoolTaskExecutor asyncTaskExecutor = new ThreadPoolTaskExecutor();
        asyncTaskExecutor.setMaxPoolSize(poolProperties.getMaxPoolSize());
        asyncTaskExecutor.setCorePoolSize(poolProperties.getCorePoolSize());
        asyncTaskExecutor.setThreadNamePrefix("async-task-thread-pool-");
        asyncTaskExecutor.initialize();
        return asyncTaskExecutor;
    }
}
